import { execSync } from 'child_process'
import path from 'path'
import { fileURLToPath } from 'url'

/**
 * 将文档部署到 本机的 Nginx
 */
const CONFIG = {
  nginxHtmlPath: 'E:\\SDE\\nginx-1.12.2\\html',
  sourceDistPath: '.vitepress/dist',
}

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

try {
  console.log('开始部署文档到 Nginx...')

  // 删除 nginx html 目录下的旧文件
  console.log('清理 Nginx 目录...')
  execSync(`rimraf "${CONFIG.nginxHtmlPath}"`, { stdio: 'inherit' })

  // 复制新文件
  const sourcePath = path.join(__dirname, CONFIG.sourceDistPath)
  console.log(`从 ${sourcePath} 复制到 ${CONFIG.nginxHtmlPath}...`)
  execSync(`xcopy /E /I "${sourcePath}" "${CONFIG.nginxHtmlPath}"`, { stdio: 'inherit' })

  console.log('部署完成！')
} catch (error) {
  console.error('部署失败:', error.message)
  process.exit(1)
}
